<?php
namespace Admin\Controller;

use Think\Page;
use Common\Base\ControllerBase;
use Common\Logic\Common\ArticleLogic;

class ArticleController extends ControllerBase
{
    public $L_article;

    public function _initialize()
    {
        $this->L_article = new ArticleLogic();
    }

    //分类列表
    public function category_list()
    {
        $infos = $this->L_article->getCategory(2);
        if( ! empty($infos)){
            $infos = list_to_tree($infos, 'id', 'fid');
            floatArray($infos, '_child', $rs);
            $infos = $rs;
        }
        $this->assign('infos', $infos);
        $this->display();
    }

    //分类添加
    public function category_add()
    {
        if(IS_POST){
            $rs = $this->L_article->addCategory(I('post.'));
            if (false === $rs) {
                $this->error($this->L_article->getError());
                exit;
            }
            $this->success('添加成功', U('article/category_list'));
            exit;
        }
        $this->assign('category_select', $this->L_article->getCategorySelect(-1, 'fid'));
        $this->display();
    }

    //分类修改
    public function category_edit()
    {
        if(IS_POST){
            $rs = $this->L_article->editCategory(I('post.'));
            if (false === $rs) {
                $this->error($this->L_article->getError());
            }else{
                $this->success('修改成功', U('article/category_list'));
            }
            exit;
        }
        $id = (int)I('request.id'); if($id < 1){ $this->error('参数错误'); }
        $info = $this->L_article->getCategoryByPK($id);
        if(empty($info)){ $this->error('参数错误'); }
        $this->assign('category_select', $this->L_article->getCategorySelect($info['fid'], 'fid'));
        $this->assign('info', $info);
        $this->display();
    }

    //分类删除
    public function category_del()
    {
        $rs = $this->L_article->delCategory(I('request.id'));
        if(false === $rs){
            $this->error($this->L_article->getError());
        }else{
            $this->success('删除成功', U('article/category_list'));
        }
    }

    //分类状态
    public function ajax_category_st()
    {
        $rs = $this->L_article->categorySt(I('post.id'), I('post.st'));
        if($rs === false){
            $this->ajaxReturn(genReturn('n', $this->L_article->getError()));
        }else{
            $this->ajaxReturn(genReturn('y', 'ok'));
        }
    }

    //新闻列表
    public function index()
    {
        $page = (int)I('get.p'); $page = ($page>0) ? $page : 1;
        $pageSize = 20;
        $params = I('get.');
        $infos = $this->L_article->getTableArticle(3, $params, 'id,cid,title,add_ts,st,author', $page, $pageSize);
        $pager = new Page($infos['tot'], $pageSize);
        $this->assign('infos', $infos['datas']);
        $this->assign('pager', $pager->show());
        $this->assign('params', $params);
        $this->assign('categorySelect', $this->L_article->getCategorySelect((isset($params['cid'])?intval($params['cid']):-1), 'cid'));
        $this->assign('categoryKV', $this->L_article->getCategoryKV());
        $this->display();
    }

    //添加文章
    public function add()
    {
        if(IS_POST){
            $rs = $this->L_article->addArticle(I('request.'));
            if (false === $rs) {
                $this->error($this->L_article->getError());
            }else{
                $this->success('添加成功', U('article/index'));
            }
            exit;
        }
        $this->assign('category_select', $this->L_article->getCategorySelect());
        $this->display();
    }

    //编辑文章
    public function edit()
    {
        if(IS_POST){
            $rs = $this->L_article->editArticle(I('request.'));
            if (false === $rs) {
                $this->error($this->L_article->getError());
            }else{
                $this->success('编辑成功', U('article/index'));
            }
            exit;
        }
        $id = (int)I('get.id');
        if($id < 1){ $this->error('参数错误'); }
        $info = $this->L_article->getArticleByPK($id);
        $this->assign('info', $info);
        $this->assign('category_select', $this->L_article->getCategorySelect($info['cid']));
        $this->display();
    }

    //删除文章
    public function del()
    {
        $id = (int)I('get.id');
        if($id < 1){ $this->error('参数错误'); }
        $rs = $this->L_article->m_article->delArticle(array('id'=>$id));
        if(false === $rs){
            $this->error($this->L_article->getError());
        }else{
            $this->success('删除成功', U('article/index'));
        }
    }

    //文章状态
    public function ajax_st()
    {
        $id = (int)I('post.id');
        if($id < 1){ $this->ajaxReturn(genReturn('n', '参数错误')); }
        $st = (int)I('post.st');
        if($st!==1 && $st!==0){ $this->ajaxReturn(genReturn('n', '参数错误')); }
        $rs = $this->L_article->m_article->editArticle(array('id'=>$id), array('st'=>$st));
        if($rs === false){
            $this->ajaxReturn(genReturn('n', $this->L_article->getError()));
        }else{
            $this->ajaxReturn(genReturn('y', 'ok'));
        }
    }
}